package com.biostime.springdraw.modules.sd.web;

import javax.servlet.http.HttpServletRequest;

import com.biostime.springdraw.modules.sd.entity.Moviecode;
import com.biostime.springdraw.modules.sd.service.MoviecodeService;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;

/**
 * 电影兑换码表Controller
 * @author chenweicheng
 * @version 2016-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/moviecode")
public class MoviecodeController extends BaseController {

	@Autowired
	private MoviecodeService MoviecodeService;
	
	@ModelAttribute
	public Moviecode get(@RequestParam(required=false) String id) {
		Moviecode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = MoviecodeService.get(id);
		}
		if (entity == null){
			entity = new Moviecode();
		}
		return entity;
	}
	
	@RequiresPermissions("sd:moviecode:view")
	@RequestMapping(value = {"list", ""})
	public String list(Moviecode Moviecode, HttpServletRequest request, HttpServletResponse response, Model model) {
		MoviecodeService.recoveryMovie();		
		Page<Moviecode> page = MoviecodeService.findPage(new Page<Moviecode>(request, response), Moviecode); 
		model.addAttribute("page", page);
		return "modules/sd/MoviecodeList";
	}

	@RequiresPermissions("sd:moviecode:view")
	@RequestMapping(value = "form")
	public String form(Moviecode Moviecode, Model model) {
		model.addAttribute("Moviecode", Moviecode);
		return "modules/sd/MoviecodeForm";
	}

	@RequiresPermissions("sd:moviecode:edit")
	@RequestMapping(value = "save")
	public String save(Moviecode Moviecode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, Moviecode)){
			return form(Moviecode, model);
		}
		MoviecodeService.save(Moviecode);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/moviecode/?repage";
	}
	
	@RequiresPermissions("sd:moviecode:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Moviecode Moviecode, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				MoviecodeService.delete(new Moviecode(id));
			}
		}else{
			MoviecodeService.delete(Moviecode);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/moviecode/?repage";
	}

}